#include "mycoin.h"
#include<QDebug>
#include"playscene.h".h"
//MyCoin::MyCoin(QWidget *parent)
//    :QPushButton{parent}
//{

//}
MyCoin::MyCoin(QString btnImg)
{
    QPixmap pix;
    bool ret = pix.load(btnImg);
    if(!ret)
    {
        QString infor_err = QString("图片%1加载失败了").arg(btnImg);
        qDebug()<<infor_err;
        return;
    }
    this->setFixedSize(pix.width(),pix.height());
    this->setStyleSheet("QPushButton{border:0px}");
    this->setIcon(pix);
    this->setIconSize(QSize(pix.width(),pix.height()));

    time_1=new QTimer(this);
    time_2=new QTimer(this);

    //金币
    connect(time_1,&QTimer::timeout,this,[=](){
        QPixmap pix;
        QString change_pix = QString(":/res/Coin000%1").arg(this->min++);
        pix.load(change_pix);
        this->setFixedSize(pix.width(),pix.height());
        this->setStyleSheet("QPushButton{border:0px}");
        this->setIcon(pix);
        this->setIconSize(QSize(pix.width(),pix.height()));
        if(this->min>this->max)
        {
            this->min = 1;
            time_1->stop();
            is_animation = false;

        }
    });

    //银币
    connect(time_2,&QTimer::timeout,this,[=](){
        QPixmap pix;
        QString change_pix = QString(":/res/Coin000%1").arg(this->max--);
        pix.load(change_pix);
        this->setFixedSize(pix.width(),pix.height());
        this->setStyleSheet("QPushButton{border:0px}");
        this->setIcon(pix);
        this->setIconSize(QSize(pix.width(),pix.height()));
        if(this->min>this->max)
        {
            this->max = 8;
            time_2->stop();
            is_animation = false;

        }
    });

    //判断游戏是否结束
    this->result_coin=false;

}

void MyCoin::change()
{
    if(this->direction)
    {
        time_1->start(30);
        is_animation = true;
        this->direction=false;
    }
    else if(!this->direction)
    {
        time_2->start(30);
        is_animation = true;
        this->direction=true;
    }
}

//重写了鼠标点击事件，用于在某些情况下禁用点击
void MyCoin::mousePressEvent(QMouseEvent *e)
{
    if(this->result_coin==true | this->is_animation== true)
    {
        return;
    }
    else
    {
        //让父类执行其他内容
        return QPushButton::mousePressEvent(e);
    }
}
